﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using QLNS.Models;
using DTO;
namespace QLNS.api
{
    public class NhanVienAPIController : ApiController
    {
        // GET api/<controller>
        public IEnumerable<NHANVIEN> Get()
        {
            WebData model = new WebData();
            var nhanviens = model.NHANVIENs.GetAll();
            return nhanviens;
        }

        // GET api/<controller>/5
        public string Get(int id)
        {
            return "value";
        }

        // POST api/<controller>
        public HttpResponseMessage Post(NHANVIEN item)
        {
            try
            {
                if (ModelState.IsValid)
                {                    
                    WebData model = new WebData();
                    model.NHANVIENs.InsertOnSubmit(ref item);
                    var response = Request.CreateResponse<NHANVIEN>(HttpStatusCode.Created, item);
                    return response;
                }
                else
                {
                    return Request.CreateResponse(HttpStatusCode.InternalServerError, "Model state is invalid");
                }
            }
            catch (Exception ex)
            {
                return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
            }
        }

        // PUT api/<controller>/5
        public HttpResponseMessage Put(int id, NHANVIEN item)
        {
            try
            {
                WebData model = new WebData();
                /*
                NHANVIEN _item = model.NHANVIENs.GetItemById(id);
                if (_item != null) { 

                }*/
                model.NHANVIENs.UpdateOnSubmit(item);
            }
            catch (Exception ex)
            {
                return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
            }
            return Request.CreateResponse(HttpStatusCode.OK);
        }

        // DELETE api/<controller>/5
        public void Delete(int id)
        {
        }
    }
}